package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealMapper;
    /**
     * 新增菜品和对应的口味
     *
     * @param dishDTO
     */
    @Transactional//@Transactional注解表示该方法需要事务处理,保证方法的一致性,要么全成功,要么全失败
    public void saveWithFlavor(DishDTO dishDTO) {
        // dto里面还包含了口味数据,所以另外开一个dish
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);// 额外的胃口属性将不会被复制到dish里面

        // 向菜品表插入一条数据
        dishMapper.insert(dish);// DTO里面没有id信息,所以需要手动设置
        // 插入后dish的id值已经封装在dish对象里面了(xml文件里面要先获取自增的主键)
        // 获取insert语句生成的主键
        Long dishId = dish.getId();

        // 向口味表插入n条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }


    /**
     * 批量删除菜品
     * @param ids
     */
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        // 先看看菜品是是否起售,起售不能删
        // 先判断当前菜品有没有被关联,再删,再删对应的口味
        for (Long id : ids) {
           Dish dish=dishMapper.getById(id);
           if (dish.getStatus()==StatusConstant.ENABLE){
               //当前菜品起售,不能删除
               throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
               //想抛出自定义错误异常就这样干
               //throw new 谁便写的Exception("随便写的错误信息");
           }
        }
        //剩下来的都是停售的菜品,可以删除
        //所以我们就要查一下菜品有没有被关联
        List<Long> setmealIds = setmealMapper.getSetmealIdsByDishIds(ids);
        //查询出来的套餐id,如果没有东西才能
        if (setmealIds!=null||setmealIds.size()>0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        for (Long id : ids) {
            dishMapper.deleteById(id);
            dishFlavorMapper.deleteByDishId(id);
        }


    }
}
